import 'dart:async';
import 'dart:ffi';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:lib_uikit/lib_uikit.dart';

enum GTCountdownStyle { black, gray }

///countdown倒计时
class GTCountdownV3 extends StatefulWidget {
  final Duration duration;
  final VoidCallback? onFinished;
  final bool? needCountdown;
  final GTCountdownStyle? style;

  const GTCountdownV3({
    super.key,
    required this.duration,
    this.onFinished,
    this.needCountdown,
    this.style,
  });

  @override
  State<StatefulWidget> createState() => _CountdownStateV3();
}

class _CountdownStateV3 extends State<GTCountdownV3> {
  late Duration remaining;
  Timer? timer;
  late String days, hours, minutes, seconds;
  late GTCountdownStyle _style;
  double paddingHorizontal = 2.5;
  Color backgroundColor = GTColorsV3.icon1.color, textColor = GTColorsV3.text8.color;

  @override
  void initState() {
    super.initState();
    if (widget.needCountdown ?? true) {
      startTimer();
    }
    _style = widget.style ?? GTCountdownStyle.black;
    if (_style == GTCountdownStyle.gray) {
      paddingHorizontal = 8;
      backgroundColor = GTColorsV3.neutral1.color;
      textColor = GTColorsV3.text1.color;
    }
  }

  @override
  void dispose() {
    timer?.cancel();
    super.dispose();
  }

  void startTimer() {
    remaining = widget.duration;
    timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
      setState(() {
        if (remaining.inSeconds > 0) {
          remaining -= const Duration(seconds: 1);
        } else {
          timer?.cancel();
          widget.onFinished?.call();
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    if (!(widget.needCountdown ?? true)) {
      remaining = widget.duration;
    }
    formatDuration(remaining);
    if (int.parse(days) > 0 && _style == GTCountdownStyle.black) {
      return Row(
        children: [
          _dayContainer("${days}D"),
          const SizedBox(
            width: 4,
          ),
          Text(
            ":",
            style: gtTextStyleW700(fontSize: 12, color: GTColorsV3.text3.color),
          ),
          const SizedBox(
            width: 4,
          ),
          _dayContainer("$hours:$minutes:$seconds"),
        ],
      );
    } else {
      return Row(
        children: [
          if (int.parse(days) > 0 && _style == GTCountdownStyle.gray) ...[
            _container("${days}D"),
            const SizedBox(
              width: 4,
            ),
            Text(
              ":",
              style: gtTextStyleW700(fontSize: 12, color: GTColorsV3.text3.color),
            ),
            const SizedBox(
              width: 4,
            ),
          ],
          _container(hours),
          const SizedBox(
            width: 4,
          ),
          Text(
            ":",
            style: gtTextStyleW700(fontSize: 12, color: GTColorsV3.text3.color),
          ),
          const SizedBox(
            width: 4,
          ),
          _container(minutes),
          const SizedBox(
            width: 4,
          ),
          Text(
            ":",
            style: gtTextStyleW700(fontSize: 12, color: GTColorsV3.text3.color),
          ),
          const SizedBox(
            width: 4,
          ),
          _container(seconds),
        ],
      );
    }
  }

  void formatDuration(Duration duration) {
    String twoDigits(int n) => n.toString().padLeft(2, '0');
    days = duration.inDays.toString();
    hours = twoDigits(duration.inHours.remainder(24));
    minutes = twoDigits(duration.inMinutes.remainder(60));
    seconds = twoDigits(duration.inSeconds.remainder(60));
  }

  Widget _container(String text) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 3, horizontal: paddingHorizontal),
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: BorderRadius.circular(4),
      ),
      child: Text(
        text,
        style: gtTextStyleW700(fontSize: 12, color: textColor),
      ),
    );
  }

  Widget _dayContainer(String text) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 3, horizontal: 4),
      decoration: BoxDecoration(
        color: GTColorsV3.icon1.color,
        borderRadius: BorderRadius.circular(4),
      ),
      child: Text(
        text,
        style: gtTextStyleW700(fontSize: 12, color: GTColorsV3.text8.color),
      ),
    );
  }
}
